package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.types.PlayLv2Type;
import com.cyanite.playlv2api.types.SearchResult;
import com.cyanite.playlv2api.types.TaskMini;
import com.cyanite.playlv2api.types.TripMini;
import com.cyanite.playlv2api.util.ConstantsUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class SearchResultListAdapter extends BaseAdapter implements ObservableAdapter{
    private SearchResult mResults;
    private int mLayoutToInflate;
    private LayoutInflater mInflater;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver mResourcesObserver;
    private Handler mHandler = new Handler();

    public SearchResultListAdapter(Context context,SearchResult results,RemoteResourceManager rrm){
        mInflater = LayoutInflater.from(context);
        mLayoutToInflate = R.layout.search_result_list_item;
        mRrm = rrm;
        mResourcesObserver = new RemoteResourceManagerObserver();
        mRrm.addObserver(mResourcesObserver);
        mResults = results;
    }

    @Override
    public int getCount() {
        if(mResults.getIsTrip() == true){
            return mResults.getTrips().size();
        }
        return mResults.getTasks().size();
    }

    @Override
    public PlayLv2Type getItem(int position) {
        if(mResults.getIsTrip() == true){
            return mResults.getTrips().get(position);
        }
        return mResults.getTasks().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public void removeObserver() {
        mHandler.removeCallbacks(mUpdatePhotos);
        mRrm.deleteObserver(mResourcesObserver);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(mLayoutToInflate, null);

            holder = new ViewHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.imgSearchPhoto);
            holder.name = (TextView) convertView.findViewById(R.id.tvSearchName);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        if(mResults.getIsTrip() == true){
            if(mResults.getTrips()!= null){
                TripMini trip = mResults.getTrips().get(position);
                if (trip.getPhoto() != null && !trip.getPhoto().equals(""))  {
                    Uri photoUri = Uri.parse(trip.getPhoto());
                    if (!mRrm.exists(photoUri)) {
                        mRrm.request(photoUri);
                    }
                }
                if (trip.getPhoto() != null && !trip.getPhoto().equals(""))    {
                    final Uri photoUri = Uri.parse(trip.getPhoto());
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
                        holder.photo.setImageBitmap(bitmap);
                    } catch (IOException e) {
                        holder.photo.setImageResource(R.drawable.trip_default_photo);
                    }
                }   else {
                    holder.photo.setImageResource(R.drawable.trip_default_photo);
                }
                if(trip.getName() != null){
                    holder.name.setText(trip.getName());
                }
            }
        }else{
            if(mResults.getTasks()!= null){
                TaskMini task = mResults.getTasks().get(position);

                holder.photo.setImageResource(ConstantsUtils.TaskCategoryRes.mIcon[task.getType()]);
                if(task.getInfo() != null){
                    holder.name.setText(task.getInfo().getInfo1());
                }
            }
        }
        return convertView;
    }
    
    private class RemoteResourceManagerObserver implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler.post(mUpdatePhotos);
        }
    }

    private Runnable mUpdatePhotos = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };
    
    private static class ViewHolder {
        ImageView photo;
        TextView name;
    }
}
